<template>
  <div v-show="$route.name == 'project-update'">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/project-list' }">项目管理记录</el-breadcrumb-item>
      <el-breadcrumb-item>项目编辑</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <div class="title-name">项目编辑</div>
      <el-form :model="project" ref="project" :rules="rules" label-width="120px" class="demo-saveForm">
        <el-row>
          <el-col :span="24">
            <el-form-item label="项目" prop="project_name">
              <el-input v-model="project.project_name" placeholder="请输入项目名称" class="form-item"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="部门" prop="department_id">
              <el-select v-model="project.department_id" popper-class="dropdownbox" class="form-item" placeholder="请选择部门">
                <el-option v-for="item in departmentList" :label="item.name"
                  :value="item.id" :key="item.id">
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="负责人" prop="project_user_ids">
              <el-select v-model="project.project_user_ids" popper-class="dropdownbox" multiple class="form-item" placeholder="请选择负责人">
                <el-option v-for="item in userList" :label="item.name"
                  :value="item.id" :key="item.id">
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="任务描述" prop="desc">
              <el-input type="textarea" v-model="project.desc" class="form-item"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-button type="primary" @click="handleSubmit">提交</el-button>
            <el-button  @click="back" style="margin-left: 5px">返回</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      id: '',
      userId: '',
      loading: false,
      project: {
        department_id: ''
      },
      userList: [],
      rules: {
        project_name: [{ required: true, message: '请输入项目名称', trigger: 'blur' }],
        department_id: [{ required: true, message: '请选择部门', trigger: 'change' }],
        project_user_ids: [{ required: true, message: '请选择执行人', trigger: 'change' }]
      },
      departmentList: []
    }
  },
  mounted() {
    // 获取自身信息
    this.getUser()
    this.getProjectData()
  },
  watch: {
    'project.department_id': {
      handler: function(val, oldval) {
        if (oldval !== '' && oldval !== val) {
          this.project.project_user_ids = []
        }
        this.getDevUserList(val)
      }
    }
  },
  methods: {
    // 获取开发人员
    async getDevUserList(val) {
      const params = {
        page: 1,
        pageSize: 100000,
        department_id: [val]
      }
      const { data: result } = await this.$http.get('/user', { params: params })
      if (result.status !== 200) {
        return this.$message.error(result.msg)
      }
      this.userList = result.data.map(item => {
        return {
          id: item.id.toString(),
          name: item.truename
        }
      })
    },
    // 获取用户信息
    async getUser() {
      this.userId = localStorage.getItem('task_user')
      const { data: result } = await this.$http.get('/user/' + this.userId)
      this.departmentList = result.data.department
    },
    // 获取项目
    async getProjectData() {
      if (this.$route.params.id) {
        this.id = this.$route.params.id
        localStorage.setItem('projectId', this.$route.params.id)
      } else {
        this.id = localStorage.getItem('projectId')
      }
      if (this.id) {
        this.getProject(this.id)
      }
    },
    async getProject(id) {
      const { data: result } = await this.$http.get('/project/' + id)
      if (result.status !== 200) {
        this.loading = false
        return this.$message.error(result.msg)
      }
      this.loading = false
      this.project = result.data
    },
    // 提交
    async handleSubmit () {
      this.$refs.project.validate(async (valid) => {
        if (!valid) return
        let data = {
          project_name: this.project.project_name,
          project_user_ids: this.project.project_user_ids.join(','),
          department_id: this.project.department_id,
          desc: this.project.desc
        }
        // 可以发起新增入库单的网络请求
        const { data: result } = await this.$http.put('/project/' + this.id, data)
        if (result.status !== 200) {
          return this.$message.error(result.msg)
        }
        this.$message.success('保存成功！')
        this.$router.push({ path: '/project-list' })
      })
    },
    // 返回
    back () {
      this.$router.push({ 'name': 'project-list' })
    }
  }
}
</script>
<style lang="less" scoped>
.title-name {
  margin-bottom: 20px;
}
.form-item {
  width:300px
}
.upload-demo {
  width: 300px;
}
</style>
